package org.vehicle.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.vehicle.dao.AlertInfoMapper;
import org.vehicle.dao.PersonInfoMapper;
import org.vehicle.dao.TripBusRecordMapper;
import org.vehicle.dao.VehicleInfoMapper;
import org.vehicle.entity.AlertInfo;
import org.vehicle.entity.PersonInfo;
import org.vehicle.entity.TripBusRecord;
import org.vehicle.entity.VehicleInfo;
import org.vehicle.service.VehicleInfoService;
import org.vehicle.service.base.BaseServiceImpl;
import org.vehicle.util.CountDeviceProtocolHandle;
import org.vehicle.util.JsonUtils;

import com.fasterxml.jackson.databind.JsonNode;
@Service
public class VehicleInfoServiceImpl extends BaseServiceImpl<VehicleInfo, Long> implements VehicleInfoService{
	@Autowired
	private VehicleInfoMapper vehicleInfoMapper;
	@Autowired
	private PersonInfoMapper personInfoMapper;
	@Autowired
	private AlertInfoMapper alertInfoMapper;
	@Autowired
	private TripBusRecordMapper tripBusRecordMapper;
	
	@Override
	public VehicleInfo findVehicleInfoByVehicleLicense(String vehicleLicense) {

		return vehicleInfoMapper.findVehicleInfoByVehicleLicense(vehicleLicense);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCheckPersonCountAndPerson(VehicleInfo vehicleInfo) {
		vehicleInfo.setCheckPersonCount(0);
		vehicleInfo.setNotScanPersonCount(0);
		vehicleInfoMapper.updateByPrimaryKeySelective(vehicleInfo);
		List<PersonInfo> personInfoList=personInfoMapper.findPersonInfoByVehicleLicense(vehicleInfo.getVehicleLicense());
		List<AlertInfo> alertInfoList=alertInfoMapper.findAlertInfoByPersonInfoList(personInfoList);
		for(AlertInfo alertInfo:alertInfoList){
			alertInfo.setPersonInfoId(null);
			alertInfoMapper.updateByPrimaryKey(alertInfo);
		}
		personInfoMapper.deletePersonInfoByVehicleInfoId(vehicleInfo.getVehicleInfoId());
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteVehicleInfo(VehicleInfo vehicleInfo) {
		List<PersonInfo> personInfoList=personInfoMapper.findPersonInfoByVehicleLicense(vehicleInfo.getVehicleLicense());
		List<AlertInfo> alertInfoList=alertInfoMapper.findAlertInfoByPersonInfoList(personInfoList);
		for(AlertInfo alertInfo:alertInfoList){
			alertInfo.setPersonInfoId(null);
			alertInfoMapper.updateByPrimaryKey(alertInfo);
		}
		personInfoMapper.deletePersonInfoByVehicleInfoId(vehicleInfo.getVehicleInfoId());
		vehicleInfoMapper.deleteByPrimaryKey(vehicleInfo.getVehicleInfoId());
		
	}
	
	@Override
	public VehicleInfo findVehicleInfoByCode(Map<String, Object> map) {
		
		return vehicleInfoMapper.findVehicleInfoByCode(map);
	}
	@Override
	public void updateCountById(Map<String, Object> map) {
		
		vehicleInfoMapper.updateCountById(map);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public String updatePersonCount(VehicleInfo vehicleInfo,String countDeviceData,String positionJson) {
		Long[] countArr=CountDeviceProtocolHandle.protocolHandle(countDeviceData);
		if(StringUtils.isEmpty(positionJson)){
			return positionJson;
		}else{
			JsonNode positionJsonNode=JsonUtils.fromJson(positionJson);
			int state=positionJsonNode.get("state").asInt();
			String logitude=positionJsonNode.get("logitude").asText();
			String latitude=positionJsonNode.get("latitude").asText();
			long tripId=positionJsonNode.get("tripId").asLong();
			if(state==1){
				int personIn=positionJsonNode.get("personIn").asInt();
				int personOut=positionJsonNode.get("personOut").asInt();
				long busId=positionJsonNode.get("busId").asLong();
				personIn=countArr[0].intValue()-personIn;
				personOut=countArr[1].intValue()-personOut;
				TripBusRecord tripBusRecord=tripBusRecordMapper.selectByPrimaryKey(busId);
				tripBusRecord.setPersonIn(personIn);
				tripBusRecord.setPersonOut(personOut);
				tripBusRecordMapper.updateByPrimaryKeySelective(tripBusRecord);
			}else if(state==2){
				if(countArr[0].intValue()-vehicleInfo.getPersonIn()>0||countArr[1].intValue()-vehicleInfo.getPersonOut()>0){
					TripBusRecord tripBusRecord=new TripBusRecord();
					tripBusRecord.setPersonIn(countArr[0].intValue()-vehicleInfo.getPersonIn());
					tripBusRecord.setPersonOut(countArr[1].intValue()-vehicleInfo.getPersonOut());
					tripBusRecord.setLogitude(logitude);
					tripBusRecord.setLatitude(latitude);
					tripBusRecord.setTripId(tripId);
					tripBusRecord.setCreateDate(new Date());
					tripBusRecordMapper.insertSelective(tripBusRecord);
					Map<String, Object> positionMap=new HashMap<String, Object>();
					positionMap.put("tripId",tripId );
					positionMap.put("busId",tripBusRecord.getTripBusId());
					positionMap.put("personIn", vehicleInfo.getPersonIn());
					positionMap.put("personOut", vehicleInfo.getPersonOut());
					positionMap.put("logitude", logitude);
					positionMap.put("latitude", latitude);
					positionMap.put("state", 1);
					positionJson=JsonUtils.toJson(positionMap);
				}
			}
		}
		vehicleInfo.setPersonCount(vehicleInfo.getPersonCount()+((countArr[0].intValue()-vehicleInfo.getPersonIn())-(countArr[1].intValue()-vehicleInfo.getPersonOut())));
		vehicleInfo.setPersonIn(countArr[0].intValue());
		vehicleInfo.setPersonOut(countArr[1].intValue());
		vehicleInfoMapper.updateByPrimaryKeySelective(vehicleInfo);
		return positionJson;
	}
	@Override
	public List<Map<String, Object>> findRunVehicleInfoList() {
		
		return vehicleInfoMapper.findRunVehicleInfoList();
	}
	@Override
	public Map<String, Object> getDeviceInfoPage(Map<String, Object> paramMap) {
		List<VehicleInfo> deviceInfoList = vehicleInfoMapper.getDeviceInfoList(paramMap);
    	Integer count = vehicleInfoMapper.getDeviceInfoCount(paramMap);
    	//分页数据
    	Map<String, Object> dataMap = new HashMap<String, Object>();
    	dataMap.put("totalCount", count);
    	dataMap.put("list", deviceInfoList);
    	dataMap.put("currentPage", paramMap.get("currentPage"));
		return dataMap;
	}
	
    
}